package org.noote.roby.core;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.noote.libs.camera.CameraInterface;
import org.noote.libs.camera.listener.CameraPreviewListener;
import org.noote.libs.graphicbuffer.GraphicBuffer;

import android.content.Context;

public class CameraPreview extends org.noote.libs.camera.preview.CameraPreview {

	private WorldView _worldView = null;
	protected  final Lock _cameraPreviewLock = new ReentrantLock();
	
	public CameraPreview(Context context, CameraInterface camera, WorldView worldView) {
		super(context, camera);
		
		this._worldView = worldView;

		getCamera().setPreviewListener(new CameraPreviewListener()
		{
			@Override
			public void onStartPreview() {
			}
	
			@Override
			public void onGrabbedFrame(GraphicBuffer frame) {
				if (_cameraPreviewLock.tryLock()) {
					try {
						_worldView.copy(frame.getBuffer(), frame.getWidth(), frame.getHeight());
					} finally {
						_cameraPreviewLock.unlock();
					}
				}
			}
	
			@Override
			public void onStopPreview() {
			}
		});
	}
}
